//
//  XSJTestRefreshControl.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/18.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit

enum TestRefreshState : Int {
    
    case Normal = 0
    case Pulling = 1
    case Refreshing = 2
    
}

class XSJTestRefreshControl: UIControl {
    
    var testRefreshState: TestRefreshState = .Normal {
        didSet{
            
            switch testRefreshState {
                
            case .Normal:
                print(".noraml")
                // 只有上一次是刷新状态才去-=50
                if oldValue == .Refreshing {
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        self.currentScrollView?.contentInset.top -= 50
                        
                    })
                }
                
                
            case .Pulling:
                print(".pulling")
            case .Refreshing:
                print("refreshing")
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.currentScrollView?.contentInset.top += 50
                })
                
                //  告诉使用者要刷新数据
                sendActionsForControlEvents(.ValueChanged)
                
                
            }
            
            
            
        }
    }
    var currentScrollView: UIScrollView?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor.yellowColor()
        
        
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        guard let scrollView = newSuperview as? UIScrollView else {
            
            return
            
        }
        scrollView.addObserver(self, forKeyPath: "contentOffset", options: [.New], context: nil)
        currentScrollView = scrollView
        
        frame.size.height = 50
        frame.size.width = currentScrollView!.frame.size.width
        frame.origin.y = -50
        
    }
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if currentScrollView!.dragging {
            
            let maxY = -(currentScrollView!.contentInset.top + 50)
            
            let contentOffsetY = currentScrollView!.contentOffset.y
            
            if contentOffsetY < maxY && testRefreshState == .Normal{
            
                testRefreshState = .Pulling
                
            }else if contentOffsetY >= maxY && testRefreshState == .Pulling{
            
                testRefreshState = .Normal
                
            }
            
        }else{
        
            if testRefreshState == .Pulling {
            
                testRefreshState = .Refreshing
                
            }
            
        }
    }
    
    func endRefreshing() {
        testRefreshState = .Normal
    }
    
    deinit {
        
        currentScrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }

    
}
